﻿using JsonArchive;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HousekeepingSystem
{
    public partial class Form4_1 : Form
    {
        public Form4_1()
        {
            InitializeComponent();
            LoadInfo();
        }

        private Dictionary<string, object> ContractTable = new Dictionary<string, object>();
        private Dictionary<string, object> EmployeeList = new Dictionary<string, object>();

        private void LoadInfo()
        {
            SetContractTable();
            SetEmployeeList();
        }
        private void SetContractTable()
        {
            ContractTable = J2D.JsonToDictionary(File.ReadAllText(Application.StartupPath + @"\info\ContractTable.json"));
            comboBox1.BeginUpdate();
            comboBox1.Items.Clear();
            HashSet<object> address = new HashSet<object>();
            foreach (var item in ContractTable)
            {
                if (item.Key == "0") continue;
                address.Add(((ArrayList)item.Value)[1]);
            }
            comboBox1.DataSource = address.ToList();
            comboBox1.EndUpdate();
            comboBox1.SelectedIndex = 0;
        }
        private void SetEmployeeList(string RetrievalStr = "")
        {
            EmployeeList = J2D.JsonToDictionary(File.ReadAllText(Application.StartupPath + @"\info\EmployeeList.json"));
            listBox1.BeginUpdate();
            listBox1.Items.Clear();
            foreach (var item in EmployeeList)
            {
                if (item.Key == "0") continue;
                listBox1.Items.Add(((ArrayList)item.Value)[0]);
            }
            listBox1.EndUpdate();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var address = comboBox1.SelectedItem;
            comboBox2.BeginUpdate();
            comboBox3.BeginUpdate();
            ArrayList Doorplate = new ArrayList();
            ArrayList CallList = new ArrayList();
            foreach (var item in ContractTable)
            {
                if (item.Key == "0") continue;
                if(address.Equals(((ArrayList)item.Value)[1]))
                {
                    Doorplate.Add(((ArrayList)item.Value)[2]);
                    CallList.Add(((ArrayList)item.Value)[5]);
                }
            }
            comboBox2.DataSource = Doorplate;
            comboBox3.DataSource = CallList;
            comboBox2.EndUpdate();
            comboBox3.EndUpdate();
            comboBox2.SelectedIndex = 0;
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox3.Items.Count < 1) return;
            comboBox3.SelectedIndex = comboBox2.SelectedIndex;
        }

        private void listBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (listBox1.SelectedItem == null) return;
            listBox2.Items.Add(listBox1.SelectedItem);
            listBox1.Items.Remove(listBox1.SelectedItem);
        }

        private void listBox2_MouseClick(object sender, MouseEventArgs e)
        {
            if (listBox2.SelectedItem == null) return;
            listBox1.Items.Add(listBox2.SelectedItem);
            listBox2.Items.Remove(listBox2.SelectedItem);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkDispatchTable()) return;
            saveDispatchTable();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        //检查派工单
        private bool checkDispatchTable()
        {
            if (listBox2.Items.Count < 1)
            {
                listBox2.Focus();
                MessageBox.Show("派工名单不能为空!","错误");
                return true;
            }
            return false;
        }
        //保存派工单
        private void saveDispatchTable()
        {
            String JSON = File.ReadAllText(Application.StartupPath + @"\info\DispatchTable.json");
            var DispatchTable = J2D.JsonToDictionary(JSON);

            ArrayList DispatchValue = new ArrayList();
            DispatchValue.Add(DateTime.Now.ToString("yyyy-MM-dd"));
            DispatchValue.Add("未完成");
            DispatchValue.Add(comboBox1.SelectedItem);
            DispatchValue.Add(comboBox2.SelectedItem);
            DispatchValue.Add(comboBox3.SelectedItem);
            List<string> namelist = new List<string>();
            foreach (var item in listBox2.Items)
            {
                namelist.Add(item.ToString());
            }
            DispatchValue.Add(string.Join("|",namelist));
            int lastTable = Convert.ToInt32(DispatchTable.Keys.Last());
            DispatchTable.Add((lastTable + 1).ToString(), DispatchValue.ToArray());

            JSON = D2J.DictionaryToJson(DispatchTable);
            File.WriteAllText(Application.StartupPath + @"\info\DispatchTable.json", JSON);
        }
        private void Form4_1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                this.button1_Click(sender, e);
                e.Handled = true;
            }
            if (e.KeyChar == (char)Keys.Escape)
            {
                this.button2_Click(sender, e);
                e.Handled = true;
            }
        }
    }
}
